﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Web7.Logging;

namespace Web7.ExceptionHandling
{
	public class LogExceptionPublisher : IExceptionPublisher
	{
		public void Publish(Exception ex)
		{

			string message = "异常消息未予以显示";
			string stackTrace = "异常的调用堆栈未予以显示";
			string formatNotice = "系统于{0}发生了异常：\r\n{1}\r\n\r\n错误堆栈跟踪信息：\r\n{2}";

			message = ex.Message;
			stackTrace = ex.StackTrace;


			string text = string.Format(formatNotice, DateTime.Now, message, stackTrace);

			Console.WriteLine(text);
			System.Diagnostics.Debug.WriteLine(text);

			var type = ex.TargetSite.DeclaringType;
			var logger = LogHelper.LoggerFor(type);
			if(null != logger)
			{
				logger.Error(text, ex);
			}
		}

		public void Initialize(System.Collections.Specialized.NameValueCollection parameters)
		{

		}
	}
}
